package com;

import obj.Course;
import util.JdbcUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;


@WebServlet(value = "/InsertCourse")
public class InsertCourse extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
         doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        Connection con=null;
        PreparedStatement psm1=null;

        try {
        con= JdbcUtil.getConnection();
        PreparedStatement psm2=null;
        ResultSet rs=null;
        psm2=con.prepareStatement("SELECT * FROM course");
        rs=psm2.executeQuery();
        ArrayList list =new ArrayList();

        while (rs.next()){
            int c_id=rs.getInt("c_id");
            String c_name=rs.getString("c_name");
            int t_id=rs.getInt("t_id");
            Course c=new Course(c_id,c_name,t_id);
            list.add(c);
        }
            request.setAttribute("lists",list);
            request.getRequestDispatcher("/jsp/InsertCourse.jsp").forward(request,response);

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
